package com.whut.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.whut.model.ContractTaskProject;
import com.whut.model.Equipinfo;
import com.whut.service.InfoManageService;
import com.whut.utils.FileUtil;
import com.whut.model.Contractinfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/InfoManage")
public class InfoManageController {

    @Resource
    private InfoManageService infoManageService;

    @ResponseBody
    @PostMapping("/getProjectInfo")
    public Object getProjectInfoByPagetContractClientge(String searchParams,int pageNum,int pageSize){
        if(searchParams.equals("")||searchParams == null){
            return infoManageService.getProjectInfoByPage(pageNum,pageSize);
        } else{
            return infoManageService.getProjectInfoBySearchParams(searchParams,pageNum,pageSize);
        }
    }

    @ResponseBody
    @PostMapping("selectProject")
    public Object selectProject(String searchParams){
        return infoManageService.getProjectInfoBySearchParams2(searchParams);
    }

    @ResponseBody
    @PostMapping("/deleteProjectInfo")
    public String deleteProjectInfoByPage(int projectId){
        return infoManageService.deleteProjectInfoById(projectId);
    }

    @ResponseBody
    @PostMapping("/getContractClient")
    public Object getContractClient(String params){
        return infoManageService.getContractClient();
    }

    @ResponseBody
    @PostMapping("/getClientByContractId")
    public Object getClientByContractId(int contractId){
        return infoManageService.getClientByContractId(contractId);
    }


    @ResponseBody
    @PostMapping("/addProjectInfo")
    public Object addProjectInfo(ContractTaskProject info){
        return infoManageService.addProjectInfo(info);
    }

    @ResponseBody
    @PostMapping("/updateProjectInfo")
    public Object updateProjectInfo(ContractTaskProject info){
        return infoManageService.updateProjectInfo(info);
    }

    @ResponseBody
    @PostMapping("/importExcel")
    public String importExcel(@RequestParam("file") MultipartFile file){
        Date date = new Date();
        String time = new SimpleDateFormat("yyyyMMddHHmmss").format(date);
        System.out.println(time);
        String resultPath = null;
        try{
            /*   上传文件路劲：C:\Tomcat8\apache-tomcat-8.5.41\webapps\yirui_sc_war/upload/importfiles/    */
            resultPath = FileUtil.upload(file.getInputStream(), "importfiles/", time+file.getOriginalFilename());
            System.out.println(resultPath);
            File f = new File(resultPath+time+file.getOriginalFilename());
            String filePath = resultPath+time+file.getOriginalFilename();
            System.out.println("filepath"+filePath);
            return infoManageService.importExcel(filePath);
        }catch(Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    @ResponseBody
    @PostMapping("/importExcel2")
    public String importExcel2(@RequestParam("file") MultipartFile file){
        Date date = new Date();
        String time = new SimpleDateFormat("yyyyMMddHHmmss").format(date);
        System.out.println(time);
        String resultPath = null;
        try{
            /*   上传文件路劲：C:\Tomcat8\apache-tomcat-8.5.41\webapps\yirui_sc_war/upload/importfiles/    */
            resultPath = FileUtil.upload(file.getInputStream(), "importfiles2/", time+file.getOriginalFilename());
            System.out.println(resultPath);
            File f = new File(resultPath+time+file.getOriginalFilename());
            String filePath = resultPath+time+file.getOriginalFilename();
            System.out.println("filepath"+filePath);
            return infoManageService.importExcel2(filePath);
        }catch(Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    @ResponseBody
    @PostMapping("getEquipInfo")
    public Object getEquipInfo(int projectId){
        return infoManageService.getEquipInfo(projectId);
    }

    /*wyh*/
    /*通过合同id获取所有项目信息*/
    @ResponseBody
    @PostMapping("/getProjectInfobyContractId")
    public Object getProjectInfobyContractId(int contractId){
        return infoManageService.getProjectInfobyContractId(contractId);
    }

    @ResponseBody
    @PostMapping("/finishZhouqi")
    public String finishZhouqi(int taskId,String finishTime)
    {
        return infoManageService.finishZhouqi(taskId,finishTime);
    }

    @ResponseBody
    @PostMapping("/saveEquip")
    public Object saveEquip(Equipinfo equipinfo){
        return infoManageService.saveEquip(equipinfo);
    }

    @ResponseBody
    @PostMapping("/editContractInfo")
    public boolean editContractInfo(Contractinfo  ci){
        return infoManageService.editContractInfo(ci);
    }


    /**
     * @Author chenhuanru
     * @Description //获取登录者的项目信息
     * @Date 11:46 2020/6/29
     * @Param [contractId, pageNum, pageSize]
     * @return java.lang.Object
     **/
    @ResponseBody
    @PostMapping("/getDataByContractId")
    public Object getDataByContractId(Integer contractId,int pageNum,int pageSize){
        System.out.println(contractId);
        if(contractId == null){
            return infoManageService.getProjectInfoByPage(pageNum,pageSize);
        } else{
            return infoManageService.getDataByContractId(contractId,pageNum,pageSize);
        }
    }

    /**
     * @Author chenhuanru
     * @Description //模糊数学统计：根据委托方id(可能多个)查询建设单位
     * @Date 17:26 2020-10-29 17:26:13
     * @Param [contractId]
     * @return java.lang.Object
     **/
    @ResponseBody
    @PostMapping("/getJsUnitByContractId")
    public Object getJsUnitByContractId(String contractIds){
        log.info("contractIds:{}",contractIds);
        JSONArray jsonArray = JSON.parseArray(contractIds);
        List<Integer> list = JSONObject.parseArray(jsonArray.toJSONString(), Integer.class);
        return infoManageService.getJsUnitByContractId(list);
    }
    /**
     * @Author chenhuanru
     * @Description //根据委托方id，获取施工单位
     * @Date 15:03 2020-10-30 15:03:45
     * @Param [contractIds]
     * @return java.lang.Object
     **/
    @ResponseBody
    @PostMapping("/getGsUnitByContractId")
    public Object getGsUnitByContractId(String contractIds){
        JSONArray jsonArray = JSON.parseArray(contractIds);
        List<Integer> list = JSONObject.parseArray(jsonArray.toJSONString(), Integer.class);
        return infoManageService.getGsUnitByContractId(list);
    }
    /**
     * @Author chenhuanru
     * @Description //根据委托方id，获取监理单位
     * @Date 15:41 2020-10-30 15:41:50
     * @Param [contractIds]
     * @return java.lang.Object
     **/
    @ResponseBody
    @PostMapping("/getJlUnitByContractId")
    public Object getJlUnitByContractId(String contractIds){
        JSONArray jsonArray = JSON.parseArray(contractIds);
        List<Integer> list = JSONObject.parseArray(jsonArray.toJSONString(), Integer.class);
        return infoManageService.getJlUnitByContractId(list);
    }

    /**
     * @Author chenhuanru
     * @Description //根据委托方获取片区
     * @Date 15:50 2020-10-30 15:50:47
     * @Param [contractIds]
     * @return java.lang.Object
     **/
    @ResponseBody
    @PostMapping("/getTaskByContractIds")
    public Object getTaskByContractIds(String contractIds){
        JSONArray jsonArray = JSON.parseArray(contractIds);
        List<Integer> list = JSONObject.parseArray(jsonArray.toJSONString(), Integer.class);
        return infoManageService.getTaskByContractIds(list);
    }
    /**
     * @Author chenhuanru
     * @Description //根据委托方和片区，获取项目数据
     * @Date 9:32 2020-11-05 09:32:55
     * @Param [contractIds, taskIds]
     * @return java.lang.Object
     **/
    @ResponseBody
    @PostMapping("/getProjectBYTasKIdAndContractId")
    public Object getProjectBYTasKIdAndContractId(String contractIds,String taskIds){
        JSONArray jsonArray = JSON.parseArray(contractIds);
        List<Integer> contractList = JSONObject.parseArray(jsonArray.toJSONString(), Integer.class);
        JSONArray jsonArray1 = JSON.parseArray(taskIds);
        List<Integer> taskList = JSONObject.parseArray(jsonArray1.toJSONString(), Integer.class);
        return infoManageService.getProjectBYTasKIdAndContractId(contractList,taskList);
    }

    @ResponseBody
    @PostMapping("/getProjectsByParams")
    public Object getProjectsByParams(String contractId,String jsUnit,String sgUnit,String jlUnit,String task,String projectId,int pageNum){
        //委托方
        JSONArray jsonArray = JSON.parseArray(contractId);
        List<Integer> contracts = JSONObject.parseArray(jsonArray.toJSONString(), Integer.class);
        //建设单位
        JSONArray jsUnitArray = JSON.parseArray(jsUnit);
        List<String> jsUnits = JSONObject.parseArray(jsUnitArray.toJSONString(),String.class);
        //施工单位
        JSONArray sgUnitArray = JSON.parseArray(sgUnit);
        List<String> sgUnits = JSONObject.parseArray(jsUnitArray.toJSONString(),String.class);
        //监理单位
        JSONArray jlUnitArray = JSON.parseArray(jlUnit);
        List<String> jlUnits = JSONObject.parseArray(jlUnitArray.toJSONString(),String.class);
        //片区
        JSONArray taskArray = JSON.parseArray(task);
        List<Integer> tasks = JSONObject.parseArray(taskArray.toJSONString(),Integer.class);
        //项目
        JSONArray projectArray = JSON.parseArray(projectId);
        List<Integer> projects = JSONObject.parseArray(projectArray.toJSONString(), Integer.class);

        return infoManageService.getProjectsByParams(contracts,jsUnits,sgUnits,jlUnits,tasks,projects,pageNum);
    }
}
